<template>
  <div>

    <div class="title">
      <div class="desc">代表性专利（选择后原有的选过的数据会被覆盖了）</div>
      <div>
        <el-button type="primary" @click="handleAdd" :disabled="flag == 3">添加</el-button>
      </div>
    </div>

    <el-table :data="membersList" class="table">


      <el-table-column label="专利名称"  width="140" >
        <template #default="scope">
          <el-input v-if="activeIndex == scope.$index" v-model="scope.row.patentName"
                    placeholder="请输入专利名称" ></el-input>
          <span v-else>{{ scope.row.patentName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="专业类型"  width="120" >
        <template #default="scope">
          <el-input v-if="activeIndex == scope.$index" v-model="scope.row.intellectualPropertyType"
                    placeholder="请输入专利名称" ></el-input>
          <span v-else>{{ scope.row.intellectualPropertyType }}</span>
        </template>
      </el-table-column>


      <el-table-column label="授权时间"  width="150" >
        <template #default="scope">
          <el-date-picker v-if="activeIndex == scope.$index" v-model="scope.row.authorizationTime" type="data"
                          value-format="YYYY-MM-DD" placeholder="授权时间" clearable style="width: 100%" />
          <span v-else>{{ scope.row.authorizationTime }}</span>
        </template>
      </el-table-column>


      <el-table-column label="专利号"  width="140" >
        <template #default="scope">
          <el-input v-if="activeIndex == scope.$index" v-model="scope.row.patentNumber"
                    placeholder="请输入专利号" ></el-input>
          <span v-else>{{ scope.row.patentNumber }}</span>
        </template>
      </el-table-column>


      <el-table-column label="完成单位"  width="180" >
        <template #default="scope">
          <el-input v-if="activeIndex == scope.$index" v-model="scope.row.completeUnitName"
                    placeholder="请输入完成单位"></el-input>
          <span v-else>{{ scope.row.completeUnitName }}</span>
        </template>
      </el-table-column>


      <el-table-column label="是否入库"  width="140" >
        <template #default="scope">
          <el-select v-if="activeIndex == scope.$index" v-model="scope.row.isIncluded" clearable>
            <el-option label="是" value="是"></el-option>
            <el-option label="否" value="否"></el-option>
          </el-select>
          <span v-else>{{ scope.row.isIncluded }}</span>
        </template>
      </el-table-column>

      <el-table-column align="right" width="250">
        <template #header>
          <span>操作</span>
        </template>
        <template #default="scope">
          <div v-if="activeIndex == scope.$index">

            <el-button type="success" @click="handleSave">保存</el-button>
          </div>
          <div v-else>
            <el-button type="primary" @click="handleEdit(scope.$index)" :disabled="flag == 3">编辑</el-button>
            <el-popconfirm @confirm="handleDelete(scope.$index)" width="220" confirm-button-text="确定"
                           cancel-button-text="取消" icon-color="#e6a23c" title="你确定要删除当前行?">
              <template #reference>
                <el-button type="danger" :disabled="flag == 3">删除</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog center class="global-dialog" v-model="dialogVisable" width="75%" top="10vh" :show-close="true"
               :close-on-click-modal="false">
      <template #title>
        <div class="my-header">
          <el-tabs class="demo-tabs">
            <el-tab-pane label="选择专利">
              <el-col :span="16" align="left" style="margin-bottom:15px">
                <el-input clearable v-model="search.patentId" placeholder="专利号"
                          style="width: 120px; margin-right: 15px">
                </el-input>

                <el-input clearable v-model="search.patentName" placeholder="专利名称"
                          style="width: 120px; margin-right: 15px">
                </el-input>


                <el-select clearable v-model="search.patentClassId" placeholder="专利类型">
                  <el-option :label="item.itemText" :value="item.itemValue" :key="item.itemValue"
                             v-for="item in classList" />
                </el-select>
                <el-button class="ordinary" @click="getTableList">搜索</el-button>
              </el-col>

              <div>

                <el-table :data="tableDate" v-loading="isLoading" style="width: 100%" class="center-table"
                          @selection-change="handleSelectionChange">
                  <el-table-column type="selection" width="55">
                  </el-table-column>
                  <ColumnData v-for="column of columns" :key="column.label" :item="column" />
                </el-table>
                <div class="example-pagination-block">
                  <el-pagination :current-page="pagination.page" :page-size="pagination.pageSize"
                                 background layout="prev, pager, next ,total,sizes,jumper"
                                 :total="pagination.total" />
                </div>
              </div>
            </el-tab-pane>

          </el-tabs>
        </div>
      </template>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="closeVisable">关闭</el-button>
                    <el-button type="primary" @click="choose">选择</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>


<script  lang="ts">
import PopupTreeInput from '@/views/science/components/PopupTreeInput/index.vue';
import ColumnData from '@/views/science/components/table/column-data.vue';
import { tips } from "@/views/science/components/tips/tips";
import moment from "moment";
import tableData from './approve.json';


import { getTable } from "@/views/science/api/patent";
import { reactive, ref, toRefs, watch } from 'vue';

export default {


  components: {
    ColumnData,PopupTreeInput

  },
  emits: {
    dataChange: null,
    paramsChange: null,
  },
  props: {
    members: {
      type: Array,
      required: true,
    },
    classList: {
      type: Array,
      required: true,
    },
    // falg  1:新增  2:编辑  3:详情
    flag: {
      type: String,
      default: 1,
    },
  },



  setup(props, { emit }) {

    const deptForm = {
      parentId: 0,
      parentName: '',
    }

    let data = reactive({


      unitNameList: [],
      typeList: [],
      workList: [],
      yearList: [],

      tableDate: [],
      temp: [

      ],
      classList: [],
      columns: tableData.compositionHeader,
      isLoading: false,
      pagination: { page: 1, pageSize: 10, total: 0 }, //分页
      search: {
        patentId: "",
        patentName: "",
        patentUserId: "",
        patentUserName: "",
        pyear: '',
      },


    })

    let activeIndex = ref(-1);

    let dialogVisable = ref(false);


    const membersList = ref(props.members)

    watch(props, (newValue, oldValue) => {
      membersList.value = newValue.members
    });


    const closeVisable = () => {

      dialogVisable.value = false
    }

    const handleSelectionChange = (arr) => {


      data.temp = arr.map(item => (
              { patentName: item.patentName, intellectualPropertyType: item.patentType, authorizationTime: item.getEvent, patentNumber: item.patentId, completeUnitName: item.patentUserName, isIncluded: '否' }
          )
      )
    }

    const changeUnit = (item:any) => {


      data.search.patentUserId=item.id
      data.search.patentUserName=item.Name




    }



    //  获取学术论文列表/发送请求/获取数据

    const handleAdd = () => {

      getTableList()
      dialogVisable.value = true
    }

    const getTableList = () => {
      data.isLoading = true,
          getTable({
            current: data.pagination.page,
            size: data.pagination.pageSize,
            ...data.search,
          }).then((res) => {
            let resData = res.data.data.records;

            resData.map((item) => {
              item.publicDate = moment(item.publicDate).format("YYYY-MM-DD");
            });

            data.tableDate = res.data.data.records;
            data.pagination.total = res.data.data.total;
            data.isLoading = false;
          });
    };


    const choose = () => {

      if (data.temp.length == 0) {

        tips("请至少选择一个", "warning");

        return false

      }

      membersList.value = data.temp

      activeIndex.value = -1;

      emit('paramsChange', membersList.value)

      dialogVisable.value = false

    }

    const handleEdit = (index: number) => {

      activeIndex.value = index;
    };

    const handleSave = () => {

      activeIndex.value = -1;

      emit('paramsChange', membersList.value)

    };

    const handleDelete = (index: number) => {
      membersList.value.splice(index, 1);

      emit('paramsChange', membersList.value)
    };

    return {

      membersList,
      activeIndex,
      dialogVisable,
      getTableList,
      choose,
      handleAdd,
      closeVisable,
      handleEdit,
      changeUnit,
      handleSave,
      handleDelete,
      handleSelectionChange,
      ...toRefs(data),
      deptForm
    }

  },
}
</script>












<style scoped lang="scss">
.title {
  display: flex;
  justify-content: space-between;

  .desc {
    font-weight: bolder;
  }
}


.title-line {
  color: #409EFF;
}

.dialog-footer {
  display: flex;
  justify-content: end;
}

:deep(.el-dialog__body) {
  display: none;
}

// 滚动条
:deep(.demo-tabs > .el-tabs__content) {
  max-height: 500px;
  overflow-y: scroll;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.table {
  margin: 15px 0;
}
</style>
